package com.kingyea.mobilepolice.apimanage.utils;


import com.alibaba.fastjson.JSON;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * @author cxc
 * @version v1.0.0
 * @description
 * @date Created in 2018.03.22 20:59
 */

public class ExceptionConditions {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionConditions.class);

    public static <T> T checkNotNull(T reference, String code) {
        if (reference == null) {
            BusinessException businessException = new BusinessException(code);
            System.out.println(JSON.toJSONString(businessException));
            LOGGER.warn(JSON.toJSONString(businessException));
            throw businessException;
        }
        return reference;
    }

    public static <T> T checkIsNull(T reference, String code) {
        if (reference != null) {
            BusinessException businessException = new BusinessException(code);
            LOGGER.warn(JSON.toJSONString(businessException));
            throw businessException;
        }
        return reference;
    }

    public static String checkStringNotEmpty(String reference, String code) {
        if (reference == null || reference.length() == 0) {
            BusinessException businessException = new BusinessException(code);
            LOGGER.warn(JSON.toJSONString(businessException));
            throw businessException;
        }
        return reference;
    }

    public static <T> Collection<T> checkArrayNotEmpty(Collection<T> reference, String code) {
        if (reference == null || reference.size() == 0) {
            BusinessException businessException = new BusinessException(code);
            LOGGER.warn(JSON.toJSONString(businessException));
            throw businessException;
        }
        return reference;
    }

    public static void checkArgument(boolean expression, String code) {
        if (!expression) {
            BusinessException businessException = new BusinessException(code);
            LOGGER.warn(JSON.toJSONString(businessException));
            throw businessException;
        }
    }
}
